package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.R;
import com.zzyl.serve.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 * 
 * @author liuyp
 * @date 2025-07-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    private static final String NURSING_LEVEL_LIST = "nursing:level:list";
    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        redisTemplate.delete(NURSING_LEVEL_LIST);
        return nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        redisTemplate.delete(NURSING_LEVEL_LIST);
        return nursingLevelMapper.updateById(nursingLevel);
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        redisTemplate.delete(NURSING_LEVEL_LIST);
        return nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        redisTemplate.delete(NURSING_LEVEL_LIST);
        return nursingLevelMapper.deleteById(id);
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    /**
     * 查询所有护理等级（状态为启用）
     * @return
     */
    @Override
    public List<NursingLevel> listAll() {
        //如果缓存中有数据则从缓存中取
        List<NursingLevel> list= (List<NursingLevel>) redisTemplate.opsForValue().get(NURSING_LEVEL_LIST);
        if(list!=null && !list.isEmpty()){
            return list;
        }
        //缓存中没有，则从数据库查询后缓存到redis中
        LambdaQueryWrapper<NursingLevel> wrapper = Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getStatus, 1);
        list = list(wrapper);
        redisTemplate.opsForValue().set(NURSING_LEVEL_LIST,list);
        return list;
    }
}
